package opd.servlets;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import opd.db.services.IReport1Dao;
import opd.servlets.helpers.ReportDates;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.google.gson.Gson;

/**
 * Servlet implementation class Report1
 */
@WebServlet(description = "Serves Report 1", urlPatterns = { "/Report1" })
public class Report1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	//private Date dateFrom, dateTo;
	
	private boolean isAverage = false;
	
	// ----------------------------------------------------------------------------------------------------
	
	@Autowired
	IReport1Dao report1;
	
	// ----------------------------------------------------------------------------------------------------
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Report1() {
        super();
    }

    // ----------------------------------------------------------------------------------------------------
	
	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
	    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());		
	}

	// ----------------------------------------------------------------------------------------------------
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		isAverage = request.getParameter("p0")!=null && request.getParameter("p0").equals("2");
		
		if (!isAverage){
			getDailyReports(response);
		}else if (isAverage){
			getLastMonthAverageOfDailyReport(response);
		}
	}
	
	// ----------------------------------------------------------------------------------------------------

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//Gson gson = new Gson();
		response.getWriter().write(request.getParameter("m"));
		
		if (!isAverage){
			getDailyReports(response);
		}else if (isAverage){
			getLastMonthAverageOfDailyReport(response);
		}
	}

	// ----------------------------------------------------------------------------------------------------
	
	public IReport1Dao getReport1() {
		return report1;
	}
	
	// ----------------------------------------------------------------------------------------------------
	
	public void setReport1(IReport1Dao report1) {
		this.report1 = report1;
	}
	
	// ----------------------------------------------------------------------------------------------------
	
	public void getLastMonthAverageOfDailyReport(HttpServletResponse response) throws IOException{
		Gson gson = new Gson();
		ReportDates reportDates = new ReportDates();
		opd.domain.Report1 report1Data = report1.getAverageReport1(reportDates.getAvgDateFrom(), reportDates.getAvgDateTo());
		report1Data.setWorkingdate(StringEscapeUtils.escapeJavaScript(ReportDates.getMonthNameAndYear(reportDates.getAvgDateFrom())).replace('\\', '%'));
		
		
		response.getWriter().write(gson.toJson(report1Data));
	}

	// ----------------------------------------------------------------------------------------------------
	
	public void getDailyReports(HttpServletResponse response) throws IOException{
		Gson gson = new Gson();
		ReportDates reportDates = new ReportDates();
		response.getWriter().write(gson.toJson(report1.getReport1(reportDates.getDateFrom(), reportDates.getDateTo())));
	}
}
